/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.SaleListingForm;
import com.ntb.entities.Sales;
import com.ntb.models.SalesDAO;
import com.ntb.models.impl.SalesDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class SaleListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        SaleListingForm saleListingForm = (SaleListingForm) form;

        SalesDAO saleDAO = new SalesDAOImpl();

        String action = saleListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (saleDAO.delete(saleListingForm.getSaleID()) == 0) {
                errorMessages.add("There was an error deleting this sale. You must delete all sales associated with this sale before deleting the sale.");
            } else {
                saleDAO.delete(saleListingForm.getSaleID());
                successMessages.add("1 sales out of 1 successfully deleted.");
            }
        } else if (("deleteSales").equalsIgnoreCase(action)) {
            int[] saleIDs = saleListingForm.getSaleIDs();
            int count = 0;

            int length = 0;

            try {
                length = saleIDs.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int saleID : saleIDs) {
                    if (saleDAO.delete(saleID) == 0) {
                        errorMessages.add("There was an error deleting this sale. You must delete all sales associated with this sale before deleting the sale.");
                    } else {
                        saleDAO.delete(saleID);
                        count++;
                    }
                }
                successMessages.add(count + " sales out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        String baseUrl = request.getRequestURI() + "?";

        String saleId = null;
        int paymentMode = -1;
        String cusName = null;
        String cusAddress = null;
        String cusPhone = null;
        String flatId = null;
        String flatName = null;
        String createdDateFrom = null;
        String createdDateTo = null;
        String saleStatus = null;
        int page = -1;

        saleId = saleListingForm.getId();
        saleId = "".equals(saleId) ? null : saleId;
        if (saleId != null) {
            baseUrl += "id=" + saleId + "&amp;";
        }

        saleListingForm.getMode();
        if (paymentMode > 0) {
            baseUrl += "mode=" + paymentMode + "&amp;";
        } else {
            paymentMode = -1;
        }

        cusName = saleListingForm.getCusN();
        cusName = "".equals(cusName) ? null : cusName;
        if (cusName != null) {
            baseUrl += "cusN=" + cusName + "&amp;";
        }

        cusAddress = saleListingForm.getCusA();
        cusAddress = "".equals(cusAddress) ? null : cusAddress;
        if (cusAddress != null) {
            baseUrl += "cusA=" + cusAddress + "&amp;";
        }

        cusPhone = saleListingForm.getCusP();
        cusPhone = "".equals(cusPhone) ? null : cusPhone;
        if (cusPhone != null) {
            baseUrl += "cusN=" + cusPhone + "&amp;";
        }

        flatId = saleListingForm.getFlat();
        flatId = "".equals(flatId) ? null : flatId;
        if (flatId != null) {
            baseUrl += "flat=" + flatId + "&amp;";
        }

        flatName = saleListingForm.getFlatN();
        flatName = "".equals(flatName) ? null : flatName;
        if (flatName != null) {
            baseUrl += "flatN=" + flatName + "&amp;";
        }

        createdDateFrom = saleListingForm.getDateF();
        createdDateFrom = "".equals(createdDateFrom) ? null : flatId;
        if (createdDateFrom != null) {
            baseUrl += "dateF=" + createdDateFrom + "&amp;";
        }

        createdDateTo = saleListingForm.getDateT();
        createdDateTo = "".equals(createdDateTo) ? null : createdDateTo;
        if (flatName != null) {
            baseUrl += "dateT=" + createdDateTo + "&amp;";
        }

        saleStatus = saleListingForm.getStatus();
        saleStatus = "".equals(saleStatus) ? null : saleStatus;
        if (flatName != null) {
            baseUrl += "status=" + saleStatus + "&amp;";
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<Sales> saleList = saleDAO.getList(saleId, paymentMode, cusName, cusAddress, cusPhone, flatId, flatName, createdDateFrom, createdDateTo, saleStatus, page);
        request.setAttribute("saleList", saleList);
        
        request.setAttribute("modeList", saleDAO.getPaymentModeList());
        
        request.setAttribute("statusList", saleDAO.getStatusList());

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(saleDAO.getPageCount(saleId, paymentMode, cusName, cusAddress, cusPhone, flatId, flatName, createdDateFrom, createdDateTo, saleStatus));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
